/**
 * @FileName: Package.java
 * @Author
 * @Description:
 * @Date 2016年1月11日 下午4:32:49
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Model;

import cn.hpclub.server.service.MemberOrgService;
import cn.hpclub.server.util.InterfaceUtil;

@TableBind(tableName = "knowledge_base")
public class KnowledgeBase extends Model<KnowledgeBase>{
    private static final long         serialVersionUID = 1312507976941243069L;
    private static final Logger       logger           = LoggerFactory.getLogger(MemberOrgService.class);
    public static final KnowledgeBase dao              = new KnowledgeBase();

    public String getSqlSelect(){
        return "SELECT kb.id, name, code, detail, is_gov, kb.enabled, kb.modi_date, pv_count, ( SELECT GROUP_CONCAT(displayname) FROM knowledge_base_org kbo, organization org WHERE kbo.org_id = org.id AND kb.id = kbo.knowledge_id ) AS city_zone, ( SELECT user_name FROM sys_user WHERE kb.created_by = sys_user.id ) AS created, ( SELECT scv. NAME FROM sys_code_value scv, sys_code_type sct WHERE scv.type_id = sct.id AND sct.code = 'KNOWLEDGE_BASE_TYPE' AND kb.type = scv.value ) AS type ";
    }

    public String getSqlSelect4Export(){
        return "SELECT kb.id, name, code, detail, (CASE is_gov WHEN 1 THEN '是' WHEN 0 THEN '否' END) AS is_gov, DATE_FORMAT(kb.modi_date, '%Y-%m-%d %T') as modi_date, pv_count, ( SELECT GROUP_CONCAT(displayname) FROM knowledge_base_org kbo, organization org WHERE kbo.org_id = org.id AND kb.id = kbo.knowledge_id ) AS city_zone, ( SELECT user_name FROM sys_user WHERE kb.created_by = sys_user.id ) AS created, ( SELECT scv. NAME FROM sys_code_value scv, sys_code_type sct WHERE scv.type_id = sct.id AND sct.code = 'KNOWLEDGE_BASE_TYPE' AND kb.type = scv.value ) AS type ";
    }

    public String getSqlExceptSelect(ConcurrentHashMap<String, String> searchInfo){
        int isGov = InterfaceUtil.getGovEnterpriseFlag();
        logger.info("isGov=" + isGov);
        String selectExpect = " FROM knowledge_base kb WHERE created_org_id=" + InterfaceUtil.getOrgId()
                + " AND is_gov=" + isGov;
        for(Entry<String, String> item : searchInfo.entrySet()){
            selectExpect += buildQuery(item);
        }
        selectExpect += " ORDER BY modi_date DESC";
        logger.debug("sqlExceptSelect = " + selectExpect);
        return selectExpect;
    }

    public KnowledgeBase getModelByName(String name, int isGovEnterprise, int createdOrgId){
        String sql = "SELECT * FROM knowledge_base WHERE name=? AND is_gov=? AND created_org_id=?";
        return findFirst(sql, name, isGovEnterprise, createdOrgId);
    }

    private String buildQuery(Entry<String, String> item){
        String sql = "";
        if(item.getKey().equals("code")){
            sql += " and  code " + " like '%" + item.getValue() + "%'";
        } else if(item.getKey().equals("name")){
            sql += " and  name " + " like '%" + item.getValue() + "%'";
        } else if(item.getKey().equals("type_id")){
            // 0==全部，查询不加限制
            if(!"0".equals(item.getValue())){
                sql += " and  type=" + item.getValue();
            }
        } else if(item.getKey().equals("city_id")){
            int topOrgId = InterfaceUtil.getIdTop();
            // topOrgId==全省，查询不加限制
            if(!item.getValue().equals(String.valueOf(topOrgId))){
                sql += " AND id IN (SELECT knowledge_id FROM knowledge_base_org WHERE org_id=" + item.getValue() + ") ";
            }
        }
        return sql;
    }
}
